package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.utils.MD5Utils;
import com.bwie.utils.R;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.PatternDocument;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.mapper.UserMapper;
import com.bwie.service.UserService;




/**
 * @Author: chen
 * @Date:  2025/8/28 17:11
 * @Description: 
 **/

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserMapper mapper;

    @Override
    public R login(User user) {

        Pattern pattern = Pattern.compile("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$");

        Matcher matcher = pattern.matcher(user.getPassword());

        boolean matchePwd = matcher.matches();
        String encrypt = "";
        if(matchePwd){
            encrypt = MD5Utils.encrypt(user.getPassword());
            System.out.println("加密的密码是"+encrypt);
        }else{
            return R.ERROR(404,"密码格式错误");
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(user.getUsername())){
            wrapper.lambda().or().eq(User::getUsername,user.getUsername());
        }

        if(StringUtils.isNotEmpty(user.getPhone())){
            //正则表达式
            Pattern compile = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$");
            //表达式和值进行校验
            Matcher matcher1 = compile.matcher(user.getPhone());
            //返回结果
            boolean matches = matcher1.matches();
            if(matches){
                wrapper.lambda().or().eq(User::getPhone,user.getPhone());
            }else{
                return R.ERROR(404,"手机号格式错误");
            }
        }

        if(StringUtils.isNotEmpty(user.getUserEmail())){
            wrapper.lambda().or().eq(User::getUserEmail,user.getUserEmail());
        }
        User one = mapper.selectOne(wrapper);

        if(one == null){
            return R.ERROR(404,"用户不存在");
        }

        if(!one.getPassword().equals(encrypt)){
            return R.ERROR(404,"密码错误");
        }


        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie")
                .claim("userId", one.getUserId())
                .claim("phone",one.getPhone()).compact();


        redisTemplate.opsForValue().set("token:"+one.getUserId(),token,30, TimeUnit.MINUTES);

        one.setPassword(null);
        one.setToken(token);
        return R.OK(one);

    }

    @Override
    public R getUserList() {

        List<User> userList = mapper.selectList(null);
        return R.OK(userList);
    }
}
